﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.Unity;
using Vit.Web.Services;
using Vit.Web.Data;

namespace Vit.Web.Validation
{
    public abstract class ValidatorBase<T> : IValidator<T>
    {
        #region IValidator Members

        public abstract ValidationState Validate(T data, DataOperation dataOperation);

        #endregion

        protected ValidationError CreateValidationError(object value, string validationKey, string validationMessage, params object[] validationMessageParameters)
        {
            if (validationMessageParameters != null && validationMessageParameters.Length > 0)
            {
                validationMessage = string.Format(validationMessage, validationMessageParameters);
            }

            IUnityContainer unity = ((VitApplication)HttpContext.Current.ApplicationInstance).UnityContainer;
            ILocalizationService localize = unity.Resolve<ILocalizationService>();
            string error = localize.Resource(validationKey);
            if(string.IsNullOrEmpty(error))
                error = validationMessage;

            return new ValidationError(
                validationKey,
                value,
                new InvalidOperationException(error)
                );
        }
    }
}
